package palmtop.com.palmtop.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import net.grandcentrix.tray.AppPreferences;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.activity.DistributeActivity;
import palmtop.com.palmtop.activity.DistributeGridActivity;
import palmtop.com.palmtop.activity.HisgridtaskActivity;
import palmtop.com.palmtop.activity.HistaskActivity;
import palmtop.com.palmtop.activity.MytaskActivity;
import palmtop.com.palmtop.activity.WaittaskActivity;


public class TaskFragment extends Fragment {
    @BindView(R.id.downtaskBtn)
    ImageButton downtaskBtn;
    @BindView(R.id.distaskLayout)
    LinearLayout distaskLayout;
    @BindView(R.id.waittaskBtn)
    ImageButton waittaskBtn;
    @BindView(R.id.waittaskLayout)
    LinearLayout waittaskLayout;
    @BindView(R.id.histaskBtn)
    ImageButton histaskBtn;
    @BindView(R.id.histaskLayout)
    LinearLayout histaskLayout;
    @BindView(R.id.mytaskBtn)
    ImageButton mytaskBtn;
    @BindView(R.id.mytaskLayout)
    LinearLayout mytaskLayout;
    @BindView(R.id.mygrids)
    GridLayout mygrids;


//    private OnFragmentInteractionListener mListener;
    private Unbinder unbinder;
    String role_id;
    String ssgrid;

    public TaskFragment() {
        // Required empty public constructor
    }


    // TODO: Rename and change types and number of parameters
    public static TaskFragment newInstance(String param1, String param2) {
        TaskFragment fragment = new TaskFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.fragment_task, container, false);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_task, null);
        //返回一个Unbinder值（进行解绑），注意这里的this不能使用getActivity()
        unbinder = ButterKnife.bind(this, view);
        init(view);
        return view;
    }

    private void init(View view) {
        AppPreferences appPreferences = new AppPreferences(getActivity());
        //permission = appPreferences.getString("permission","");
        String username = appPreferences.getString("loginname","");
        String userid = appPreferences.getString("userid","");
        role_id = appPreferences.getString("role_id","");
        ssgrid = appPreferences.getString("ssgrid","");
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            waittaskLayout.setVisibility(View.GONE);
            mytaskLayout.setVisibility(View.GONE);
            mygrids.removeView(waittaskLayout);
            mygrids.removeView(mytaskLayout);

        }else if(role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")){
            mytaskLayout.setVisibility(View.GONE);
            mygrids.removeView(mytaskLayout);

        }else{
            distaskLayout.setVisibility(View.GONE);
            waittaskLayout.setVisibility(View.GONE);
            histaskLayout.setVisibility(View.GONE);
            mygrids.removeView(distaskLayout);
            mygrids.removeView(waittaskLayout);
            mygrids.removeView(histaskLayout);


        }

    }


    /*// TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
*/
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.downtaskBtn)
    public void onDowntaskBtnClicked() {
        Intent i = new Intent();
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            i.setClass(getActivity(), DistributeActivity.class);
        }else{
            i.setClass(getActivity(), DistributeGridActivity.class);
        }

        startActivity(i);
    }

    @OnClick(R.id.waittaskBtn)
    public void onWaittaskBtnClicked() {
        Intent i = new Intent();
        i.setClass(getActivity(), WaittaskActivity.class);
        startActivity(i);
    }

    @OnClick(R.id.histaskBtn)
    public void onHistaskBtnClicked() {
        Intent i = new Intent();
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            i.setClass(getActivity(), HistaskActivity.class);
        }else{
            i.setClass(getActivity(), HisgridtaskActivity.class);
        }

        startActivity(i);
    }

    @OnClick(R.id.mytaskBtn)
    public void onMytaskBtnClicked() {
        Intent i = new Intent();
        i.setClass(getActivity(), MytaskActivity.class);
        startActivity(i);
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
